/**
 * Created by Brook on 2020/4/9.
 */


(function () {
    var async = require('async');
    var sql_config = require('../../serverconfig.js');
    var model_sql = require('../../tool/db_helper.js').init(sql_config.Model_Config);
    var SQL_TXT_API = sql_config.SQL_TXT_API;


    var messageData = {
        "message": "请求成功",
        "data": {
            "code": 200,
            "data": [],
            "message": ""
        }
    };


    //修改模型名称
    exports.post_upName = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            updataName:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.upDataName, [result.check_pars.modelName, result.check_pars.projectName, result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['updataName', function (cb, result) {
                if (result.updataName.affectedRows == 0) {
                    messageData.data.code = 201;
                    messageData.data.message = "无效的uuid";
                } else {
                    messageData.data.message = "参数更新成功";
                };
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //调整解析优先级
    exports.post_adjustOrder = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_datatime:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_list_id, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            get_datatimeS:['get_datatime', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_list_id, [result.check_pars.uuids],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            check_dtaa:['get_datatimeS', function (cb, result) {
                if (result.get_datatime.length == 0) {
                    messageData.data.code = 201;
                    messageData.data.data = [];
                    messageData.data.message = "无效的uuid";
                    res.json(messageData);
                } else if (result.get_datatimeS.length == 0) {
                    messageData.data.code = 201;
                    messageData.data.data = [];
                    messageData.data.message = "无效的uuid";
                    res.json(messageData);
                } else {
                    cb(null, []);
                };
            }],
            up_data:['check_dtaa', function (cb, result) {
                model_sql.query(SQL_TXT_API.upadjustOrder, [result.check_pars.uuid,result.check_pars.uuids,result.check_pars.uuids,result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['up_data', function (cb, result) {
                messageData.data.data = [];
                messageData.data.message = "参数更新成功";
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //重新提交解析
    exports.post_againSubmit = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            up_list:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.upList, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            delLogs:['up_list', function (cb, result) {
                if (result.up_list.affectedRows == 0) {
                    messageData.data.code = 201;
                    messageData.data.message = "无效的uuid";
                    res.json(messageData);
                } else {
                    model_sql.query(SQL_TXT_API.delLogs, [result.check_pars.uuid],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            get_listid:['delLogs', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_list_id, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            del_attributes:['get_listid', function (cb, result) {
                model_sql.query(SQL_TXT_API.delAttributes, [result.get_listid[0].id],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            del_info:['del_attributes', function (cb, result) {
                model_sql.query(SQL_TXT_API.delInfo, [result.get_listid[0].id],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['del_info', function (cb, result) {
                messageData.data.message = "参数更新成功";
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };

}).call(this);
